package com.huawei.appmarket.service.pay.drm.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.service.pay.drm.bean.DrmCacheInfo;
import com.huawei.appmarket.service.pay.drm.bean.DrmItem;
import com.huawei.appmarket.service.pay.drm.bean.DrmSignResBean;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import o.cev;
import o.cfw;
import o.cvt;
import o.BaseRequest;
import o.dgh;
import o.diu;
import o.Logger;
import o.dnk;
import o.dof;
import o.doo;
import o.ebz;
import o.ecl;
import o.ecm;
import o.ecn.b;
import o.ecq;
import o.ecs;
import o.eos;
import o.eow;
import o.CollectCheck;
import o.esm;
import o.evh;

public class DrmSignService extends Service {
    private static final String ˊ = DrmSignService.class.getSimpleName();
    public final String LOGIN_ACTION = "login_action";
    public final String PROTOCOL_ACTION = "protocol_action";
    private final b ʽ = new b(this) {
        final /* synthetic */ DrmSignService ˎ;

        {
            this.ˎ = r1;
        }

        public void ˎ(Map map, ecl o_ecl) throws RemoteException {
            Logger.ˎ(DrmSignService.ˊ, "getSign start");
            if (map == null) {
                Logger.ˋ(DrmSignService.ˊ, "params is null!");
                o_ecl.ˋ(ecs.ˎ());
                this.ˎ.ˊ("8");
            } else if (o_ecl == null) {
                Logger.ˋ(DrmSignService.ˊ, "callback is null");
            } else if (diu.ˊ().ˎ()) {
                this.ˎ.ˋ(map, o_ecl);
            } else {
                Map ˏ;
                Logger.ˎ(DrmSignService.ˊ, "not agree protocol");
                if (map.containsKey("supportPrompt")) {
                    ˏ = ecs.ˏ(this.ˎ.getAction("protocol_action"), ((Boolean) map.get("supportPrompt")).booleanValue());
                } else {
                    ˏ = ecs.ˏ(this.ˎ.getAction("protocol_action"), true);
                }
                this.ˎ.ˊ("3");
                o_ecl.ˋ(ˏ);
            }
        }

        public void ˏ(Map map) throws RemoteException {
            Logger.ˎ(DrmSignService.ˊ, "report");
            try {
                Iterator it = map.entrySet().iterator();
                if (it != null) {
                    while (it.hasNext()) {
                        Entry entry = (Entry) it.next();
                        if (entry != null) {
                            String str = (String) entry.getKey();
                            String str2 = (String) entry.getValue();
                            if (Logger.ˎ()) {
                                Logger.ˏ(DrmSignService.ˊ, "report key=" + str);
                            }
                            dgh.ॱ(this.ˎ.getApplicationContext(), str, str2);
                            return;
                        }
                    }
                }
            } catch (Exception e) {
                Logger.e(DrmSignService.ˊ, "drm report exception : " + e.toString());
            }
        }
    };
    private final long ˋ = DNKeeperConfig.SEVEN_DAYS;
    private String ˎ;
    private boolean ˏ = true;
    private String ॱ;
    private String ᐝ;

    public interface e {
        void ˋ(String str, int i);
    }

    class a implements e {
        final /* synthetic */ DrmSignService ˎ;
        private final ecl ॱ;

        public a(DrmSignService drmSignService, ecl o_ecl) {
            this.ˎ = drmSignService;
            this.ॱ = o_ecl;
        }

        public void ˋ(String str, int i) {
            Logger.ˎ(DrmSignService.ˊ, "onGetValue errCode=" + i);
            if (i == 3002) {
                try {
                    Logger.ˎ(DrmSignService.ˊ, "onGetValue errCode=ERROR_KEYBACK");
                    this.ॱ.ˋ(ecs.ˋ());
                } catch (Throwable e) {
                    Logger.ˋ(DrmSignService.ˊ, "GetUserIdCallback cancel login RemoteException", e);
                }
                this.ˎ.ˊ(HwAccountConstants.TYPE_SECURITY_PHONE);
            } else if (dof.ˋ(str)) {
                Logger.ˎ(DrmSignService.ˊ, "onGetValue userId is blank.");
                if (doo.ˊ(this.ˎ.getApplicationContext())) {
                    try {
                        Logger.ˎ(DrmSignService.ˊ, "onGetValue go to login page.");
                        this.ॱ.ˋ(ecs.ˊ(this.ˎ.getAction("login_action"), this.ˎ.ˏ));
                        return;
                    } catch (Throwable e2) {
                        Logger.ˋ(DrmSignService.ˊ, "OnGetUserIdCallback getShowLoginMap userId is blank", e2);
                        return;
                    }
                }
                Map ˊ = ecs.ˊ();
                try {
                    Logger.ˎ(DrmSignService.ˊ, "onGetValue network not connected.");
                    this.ॱ.ˋ(ˊ);
                } catch (Throwable e22) {
                    Logger.ˋ(DrmSignService.ˊ, "OnGetUserIdCallback userId is blank", e22);
                }
                this.ˎ.ˊ("1");
            } else {
                try {
                    Logger.ˎ(DrmSignService.ˊ, "onGetValue userId is not blank.");
                    this.ˎ.ᐝ = str;
                    DrmCacheInfo ˊ2 = ecq.ˊ(this.ˎ.ᐝ, this.ˎ.ˎ, this.ˎ.ॱ);
                    if (ˊ2 == null) {
                        Logger.ˎ(DrmSignService.ˊ, "onGetValue getItemByCondition is null.");
                        if (doo.ˊ(this.ˎ.getApplicationContext())) {
                            try {
                                this.ॱ.ˋ(ecs.ˊ(this.ˎ.getAction("login_action"), this.ˎ.ˏ));
                                Logger.ˎ(DrmSignService.ˊ, "onGetValue go to login page.");
                                return;
                            } catch (Throwable e222) {
                                Logger.ˋ(DrmSignService.ˊ, "OnGetUserIdCallback getShowLoginMap userId is blank", e222);
                                return;
                            }
                        }
                        this.ॱ.ˋ(ecs.ˊ());
                        Logger.ˎ(DrmSignService.ˊ, "onGetValue network not connected.");
                        return;
                    }
                    Logger.ˎ(DrmSignService.ˊ, "onGetValue getItemByCondition is not null.");
                    this.ˎ.ॱ(this.ॱ, ˊ2);
                } catch (Throwable e2222) {
                    Logger.ˋ(DrmSignService.ˊ, "OnGetUserIdCallback RemoteException", e2222);
                }
            }
        }
    }

    public static class c extends eow {
        private final e ˏ;

        public c(e eVar) {
            this.ˏ = eVar;
        }

        public void onError(ErrorStatus errorStatus) {
            eos.ˎ("drm_login_type");
            int errorCode = errorStatus.getErrorCode();
            Logger.ˋ(DrmSignService.ˊ, "AccountLoginHandler onError " + errorCode + " ===  " + errorStatus.getErrorReason());
            super.onError(errorStatus);
            this.ˏ.ˋ(null, errorCode);
        }

        public String ˊ() {
            return "getAccountsByType";
        }

        public void onFinish(CloudAccount[] cloudAccountArr) {
            Logger.ˎ(DrmSignService.ˊ, "AccountLoginHandler onFinish");
        }

        public void onLogout(CloudAccount[] cloudAccountArr, int i) {
            Logger.ˎ(DrmSignService.ˊ, "AccountLoginHandler onLogout");
        }

        public void onLogin(CloudAccount[] cloudAccountArr, int i) {
            Logger.ˎ(DrmSignService.ˊ, "AccountLoginHandler onLogin");
            eos.ˎ("drm_login_type");
            String str = null;
            if (!(cloudAccountArr == null || cloudAccountArr.length < 1 || i == -1)) {
                if (Logger.ˎ()) {
                    Logger.ˏ(DrmSignService.ˊ, "getCorrectAccount : mAccounts.length = " + cloudAccountArr.length);
                }
                CloudAccount cloudAccount = cloudAccountArr[i];
                if (cloudAccount != null) {
                    Bundle accountInfo = cloudAccount.getAccountInfo();
                    if (accountInfo != null) {
                        str = accountInfo.getString("userId");
                    }
                }
            }
            this.ˏ.ˋ(str, 0);
        }
    }

    class d implements IServerCallBack {
        private final ecl ˊ;
        private boolean ˏ = true;
        final /* synthetic */ DrmSignService ॱ;

        public d(DrmSignService drmSignService, ecl o_ecl) {
            this.ॱ = drmSignService;
            this.ˊ = o_ecl;
        }

        public void ˏ(boolean z) {
            this.ˏ = z;
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
            Logger.ˎ(DrmSignService.ˊ, "notifyResult resBean.getResponseCode()=" + responseBean.getResponseCode() + " resBean.getRtnCode_()=" + responseBean.getRtnCode_());
            if (responseBean.getResponseCode() == 0) {
                if (responseBean.getRtnCode_() == 0) {
                    if (ˊ((DrmSignResBean) responseBean)) {
                        return;
                    }
                } else if (responseBean.getRtnCode_() == -3) {
                    Logger.ˎ(DrmSignService.ˊ, "resBean.getRtnCode_() == DrmConstants.StoreRtnCode.ORDER_RELATIONSHIP_ERROR");
                    ecq.ˋ(this.ॱ.ᐝ, this.ॱ.ˎ, this.ॱ.ॱ);
                    ebz.ॱ().ˋ(this.ॱ.ˎ);
                    if (this.ˏ) {
                        try {
                            this.ˊ.ˋ(ecs.ॱ());
                        } catch (Throwable e) {
                            Logger.ˋ(DrmSignService.ˊ, "DrmCallback ORDER_RELATIONSHIP_ERROR RemoteException ", e);
                        }
                        this.ॱ.ˊ(HwAccountConstants.TYPE_TENCENT);
                        return;
                    }
                } else if (responseBean.getRtnCode_() == -4) {
                    Logger.ˎ(DrmSignService.ˊ, " TOO_MUCH_DEVICE_BIND ");
                    if (this.ˏ) {
                        try {
                            this.ˊ.ˋ(ecs.ʼ());
                            return;
                        } catch (Throwable e2) {
                            Logger.ˋ(DrmSignService.ˊ, "DrmCallback TOO_MUCH_DEVICE_BIND RemoteException ", e2);
                            return;
                        }
                    }
                }
            }
            ˏ(responseBean);
        }

        private boolean ˊ(DrmSignResBean drmSignResBean) {
            List<DrmItem> list = drmSignResBean.drmItems_;
            Map hashMap = new HashMap();
            try {
                if (!CollectCheck.isEmpty(list)) {
                    ArrayList arrayList = new ArrayList();
                    ArrayList arrayList2 = new ArrayList();
                    byte[] ˋ = evh.ˋ();
                    for (DrmItem drmItem : list) {
                        String ˋ2 = drmItem.ˋ();
                        String str = drmItem.ˊ() + "";
                        String ˏ = drmItem.ˏ();
                        arrayList.add(ecs.ˏ(ˋ2, ˏ));
                        arrayList2.add(ecs.ˎ(ˋ2, ˏ, ˋ));
                        hashMap.put("ts", str);
                    }
                    hashMap.put("signList", arrayList);
                    DrmCacheInfo drmCacheInfo = new DrmCacheInfo();
                    drmCacheInfo.setDeveloperId(this.ॱ.ॱ);
                    drmCacheInfo.setTs((String) hashMap.get("ts"));
                    drmCacheInfo.setUserId(cev.ˋ(this.ॱ.ᐝ));
                    drmCacheInfo.setPkgName(this.ॱ.ˎ);
                    drmCacheInfo.setSignList(arrayList2);
                    drmCacheInfo.setIv(dnk.ˏ(ˋ));
                    Logger.ˎ(DrmSignService.ˊ, "updateAndSave ts =" + hashMap.get("ts"));
                    ecq.ॱ(drmCacheInfo);
                }
                hashMap.put("rtnCode", "0");
                hashMap.put("appstore_version", ecs.ʽ());
                if (this.ˏ) {
                    this.ˊ.ˋ(hashMap);
                }
                return true;
            } catch (Throwable e) {
                Logger.ˋ(DrmSignService.ˊ, "DrmCallback RemoteException", e);
                return false;
            } catch (Throwable e2) {
                Logger.ˋ(DrmSignService.ˊ, "DrmCallback InvalidKeyException", e2);
                return false;
            } catch (Throwable e22) {
                Logger.ˋ(DrmSignService.ˊ, "DrmCallback NoSuchAlgorithmException", e22);
                return false;
            } catch (Throwable e222) {
                Logger.ˋ(DrmSignService.ˊ, "DrmCallback UnsupportedEncodingException", e222);
                return false;
            } catch (Throwable e2222) {
                Logger.ˋ(DrmSignService.ˊ, "DrmCallback GeneralSecurityException", e2222);
                return false;
            } catch (Throwable e22222) {
                Logger.ˋ(DrmSignService.ˊ, "DrmCallback IllegalArgumentException", e22222);
                return false;
            }
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }

        private void ˏ(ResponseBean responseBean) {
            try {
                Map ˏ = ecs.ˏ();
                if (this.ˏ) {
                    this.ˊ.ˋ(ˏ);
                }
                Logger.ˎ(DrmSignService.ˊ, "DrmCallback resBean.responseCode=" + responseBean.getResponseCode() + "  resBean.rtnCode_=" + responseBean.getRtnCode_());
                if (responseBean.getResponseCode() == 0) {
                    this.ॱ.ˊ("9");
                } else {
                    this.ॱ.ˊ("5");
                }
            } catch (Throwable e) {
                Logger.ˋ(DrmSignService.ˊ, "DrmCallback RemoteException ", e);
            }
        }
    }

    public String getAction(String str) {
        if ("login_action".equals(str)) {
            ˊ("2");
        }
        String str2 = null;
        try {
            str2 = getPackageManager().getServiceInfo(new ComponentName(this, DrmSignService.class), DnsConfig.MAX_CACHE_ENTRIES).metaData.getString(str);
        } catch (NameNotFoundException e) {
            Logger.e(ˊ, "getAction exception : " + e.toString());
        }
        return str2;
    }

    private void ˋ(Map map, ecl o_ecl) throws RemoteException {
        Logger.ˎ(ˊ, "afterAgreeProtocal");
        String str = (String) map.get("pkgName");
        String str2 = (String) map.get("developerId");
        if (dof.ˋ(str) || dof.ˋ(str2)) {
            Logger.e(ˊ, "params is invalid StringUtils.isBlank(pkgName) || StringUtils.isBlank(developerId)");
            o_ecl.ˋ(ecs.ˎ());
            str = "params is invalid,pkgName =" + str + ",developerId=" + str2;
            ˊ(HwAccountConstants.TYPE_SINA);
            Logger.ˋ(ˊ, str);
            return;
        }
        this.ˎ = str;
        this.ॱ = str2;
        this.ᐝ = UserSession.getInstance().getUserId();
        if (map.containsKey("supportPrompt")) {
            this.ˏ = ((Boolean) map.get("supportPrompt")).booleanValue();
        }
        Logger.ˎ(ˊ, "afterAgreeProtocal pkgName=" + str);
        if (dof.ˋ(this.ᐝ)) {
            Logger.ˎ(ˊ, "userId is blank.");
            if (eos.ˏ(getApplicationContext())) {
                Logger.ˎ(ˊ, "userId is blank. hasLogin=true");
                ecs.ˊ(getApplicationContext(), new c(new a(this, o_ecl)));
                return;
            } else if (doo.ˊ(getApplicationContext())) {
                o_ecl.ˋ(ecs.ˊ(getAction("login_action"), this.ˏ));
                Logger.ˎ(ˊ, "go to login page.");
                return;
            } else {
                try {
                    o_ecl.ˋ(ecs.ˊ());
                } catch (Throwable e) {
                    Logger.ˋ(ˊ, "afterAgreeProtocal hasActiveNetwork", e);
                }
                Logger.ˎ(ˊ, "afterAgreeProtocal network not connected.");
                ˊ("1");
                return;
            }
        }
        Logger.ˎ(ˊ, "userId is not blank.");
        ˋ(o_ecl);
    }

    public IBinder onBind(Intent intent) {
        Logger.ˏ(ˊ, "onBind");
        return this.ʽ;
    }

    private void ˋ(ecl o_ecl) throws RemoteException {
        DrmCacheInfo ˊ = ecq.ˊ(this.ᐝ, this.ˎ, this.ॱ);
        if (ˊ == null) {
            Logger.ˎ(ˊ, "processWhenLogined item is null");
            ˋ(o_ecl, true);
            return;
        }
        Logger.ˎ(ˊ, "processWhenLogined sendResultToDrmSdk");
        ॱ(o_ecl, ˊ);
    }

    private void ॱ(ecl o_ecl, DrmCacheInfo drmCacheInfo) throws RemoteException {
        long parseLong = Long.parseLong(drmCacheInfo.getTs());
        long currentTimeMillis = System.currentTimeMillis();
        Logger.ˎ(ˊ, "sendResultToDrmSdk. saveTs=" + parseLong);
        if (!doo.ˊ(getApplicationContext()) || currentTimeMillis - parseLong < DNKeeperConfig.SEVEN_DAYS) {
            try {
                Logger.ˎ(ˊ, "getResultFromCache. currTs - saveTs=" + (currentTimeMillis - parseLong));
                Map ˊ = ecq.ˊ(drmCacheInfo);
                if (ˊ != null) {
                    if (doo.ˊ(getApplicationContext())) {
                        Logger.ˎ(ˊ, "refresh cache invokeServer start");
                        ˋ(o_ecl, false);
                    }
                    o_ecl.ˋ(ˊ);
                    return;
                } else if (!doo.ˊ(getApplicationContext())) {
                    Logger.ˋ(ˊ, "getResultFromCache network not connected.");
                    o_ecl.ˋ(ecs.ˊ());
                    ˊ("1");
                    return;
                } else if (UserSession.getInstance().isLoginSuccessful()) {
                    Logger.ˎ(ˊ, "getResultFromCache. invokeServer");
                    ˋ(o_ecl, true);
                    return;
                } else {
                    try {
                        Logger.ˎ(ˊ, "getResultFromCache go to login page.");
                        o_ecl.ˋ(ecs.ˊ(getAction("login_action"), this.ˏ));
                        return;
                    } catch (Throwable e) {
                        Logger.ˋ(ˊ, "getResultFromCache RemoteException", e);
                        return;
                    }
                }
            } catch (UnsupportedEncodingException e2) {
                Logger.e(ˊ, "sendResultToDrmSdk UnsupportedEncodingException");
                return;
            }
        }
        try {
            Logger.ˎ(ˊ, "DrmSignDataProvider.removeItem.");
            ecq.ˋ(this.ᐝ, this.ˎ, this.ॱ);
            o_ecl.ˋ(ecs.ˊ(getAction("login_action"), this.ˏ));
        } catch (Throwable e3) {
            Logger.ˋ(ˊ, "sendResultToDrmSdk NumberFormatException", e3);
        }
    }

    private void ˋ(ecl o_ecl, boolean z) throws RemoteException {
        if (doo.ˊ(getApplicationContext())) {
            cvt o_ecm = new ecm();
            o_ecm.ˏ(this.ˎ);
            o_ecm.ˎ(this.ॱ);
            Logger.ˎ(ˊ, "start invoke store pkgName = " + this.ˎ);
            IServerCallBack dVar = new d(this, o_ecl);
            dVar.ˏ(z);
            cfw.ˎ(o_ecm, dVar);
            return;
        }
        o_ecl.ˋ(ecs.ˊ());
        ˊ("1");
        Logger.ˋ(ˊ, "invokeServer network not connected.");
    }

    public void onDestroy() {
        super.onDestroy();
    }

    private void ˊ(String str) {
        esm.ˎ(str, this.ˎ);
    }
}
